/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view.ev;

import offset.nodes.client.veditor.model.ev.DataElementView;
import offset.nodes.client.editor.view.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Element;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import offset.nodes.client.virtual.model.jcr.nodetype.ValueConstraintDefinition;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.veditor.model.DataModel;

/**
 *
 * @author Walter Lütgenau
 */
public class EnumeratedValueChooser implements DataElementView {

    DataElementViewContext context = null;
    SimpleNamespaceRegistry namespaces = SimpleNamespaceRegistry.getInstance();
    ValueConstraintDefinition[] valueConstraints;

    public EnumeratedValueChooser(ValueConstraintDefinition[] valueConstraints) {
        this.valueConstraints = valueConstraints;
    }

    /**
     * Calle by VirtualBlock, when a view shall be used to retrieve the new value for a data element.
     * @param context The virtual block and the element
     */
    public void setContext(DataElementViewContext context) {
        try {
            this.context = context;

            // retrieve some basic items used below
            String value = context.getVirtualBlock().getModel().getDocument().getText(
                    context.getElement().getStartOffset(), context.getElement().getEndOffset()-context.getElement().getStartOffset()-1);

            // display the enumerated value panel and let the user select a value
            EnumeratedValuePanel panel = new EnumeratedValuePanel(valueConstraints, value);
            SimpleDialog dialog = new SimpleDialog(null, true, panel);
            panel.setContainer(dialog);
            dialog.setSize(350,250);
            dialog.setLocation(100, 100);
            dialog.setVisible(true);
            if (dialog.getReturnStatus() == SimpleDialog.OK) {
                String selected = panel.getSelectedValue();
                substituteContent(context.getElement(), selected);
            }
           } catch (Exception ex) {
            Logger.getLogger(EnumeratedValueChooser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Substitute the content of the element with the selected enumerated value
     * @param e The element
     * @param value The value
     * @throws java.lang.Exception
     */
    protected void substituteContent(Element e, String value) throws Exception {
        StringBuffer buf = new StringBuffer();
        DataModel model = context.getVirtualBlock().getModel();
        
        buf.append(model.getStartTag(e));
        buf.append(value);
        buf.append(model.getEndTag(e));
        
        model.setOuterHTML(e, buf.toString());
    }

}
